<?php

include ("addition/role.php");

class DefaultController extends Controller {

    public function filters() {
        return CMap::mergeArray(parent::filters(), array(
                    'accessControl',
                ));
    }

    public function accessRules() {
        $role = new role();
        $isAdmin = $role->getRoleAll();
        return array(
            array('allow',
                'actions' => array('error'),
                'users' => array('*')),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions' => array('index'),
                'expression' => $isAdmin,
            ),
            array('allow',
                'actions' => array('loginSucces'),
                'expression' => "isset(Yii::app()->user->role)"
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function actionIndex() {
        $criteria = new CDbCriteria;
        $criteria->select = "*";
        $criteria->condition = "owner=:owner";
        $criteria->params = array(":owner" => '1');
        $model = NEWSFLASH::model()->findAll($criteria);
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $this->render('index', array('model' => $model));
    }

    public function actionLoginSucces() {
        $roleGroup = base64_decode(Yii::app()->user->roleGroup);
        $role = base64_decode(Yii::app()->user->role);

        if (isset(Yii::app()->user->roleGroup) && ($roleGroup == 'Admin')) {
            $this->redirect('index.php?r=PENGUASALABSCHOOL');
        } else if (isset(Yii::app()->user->roleGroup) && ($roleGroup == 'Admin TK')) {
            if (isset(Yii::app()->user->role) && ($role == "Admin TK Rawamangun")) {
                $this->redirect('index.php?r=TKRAWAMANGUN');
            }
        } else if (isset(Yii::app()->user->roleGroup) && ($roleGroup == 'Admin SMP')) {
            if (isset(Yii::app()->user->role) && ($role == "Admin SMP Rawamangun")) {
                $this->redirect('index.php?r=SMPRAWAMANGUN');
            } elseif (isset(Yii::app()->user->role) && ($role == "Admin SMP Kebayoran")) {
                $this->redirect('index.php?r=SMPKEBAYORAN');
            } elseif (isset(Yii::app()->user->role) && ($role == "Admin SMP Cibubur")) {
                $this->redirect('index.php?r=SMPCIBUBUR');
            }
        } else if (isset(Yii::app()->user->roleGroup) && ($roleGroup == 'Admin SMA')) {
            if (isset(Yii::app()->user->role) && ($role == "Admin SMA Rawamangun")) {
                $this->redirect('index.php?r=SMARAWAMANGUN');
            } elseif (isset(Yii::app()->user->role) && ($role == "Admin SMA Kebayoran")) {
                $this->redirect('index.php?r=SMAKEBAYORAN');
            } elseif (isset(Yii::app()->user->role) && ($role == "Admin SMA Cibubur")) {
                $this->redirect('index.php?r=SMACIBUBUR');
            }
        } else {
            $this->redirect('index.php');
        }
    }

    public function actionError() {
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else {
                $this->layout = 'admin';
                $this->render('error', $error);
            }
        }
    }

}